package com.wen.titledemo.app;

import android.view.View;

import com.wen.titledemo.app.base.BaseFragment;
import com.wen.titledemo.app.loading.LoadingManager;
import com.wen.titledemo.app.loading.OnLoadingListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;


/**
 * AppActivity 应用的Activity
 */

public abstract class AppFragment extends BaseFragment {
    /**
     * 加载页面
     */
    protected LoadingManager mLoadingLayout;

    /**
     * ButterKnife注解
     */
    // TODO 可以自已添加 ButterKnife
    //private Unbinder mBinder;
    @Override
    protected final void initTitle() {

    }

    /**
     * 加载布局作用在哪个View上
     */
    protected Object getLoadingParentView() {
        return mContentView;
    }

    private class AppLoadingListener extends OnLoadingListener {

        @Override
        public void setRetryEvent(View retryView) {
            setViewClick(retryView);
        }

        @Override
        public void setDataErrorEvent(View errorDataView) {
            setViewClick(errorDataView);
        }

        @Override
        public int generateEmptyLayoutId() {
            int emptyLayoutId = getEmptyLayoutId();
            if (emptyLayoutId != -1) {
                return emptyLayoutId;
            }
            return super.generateEmptyLayoutId();
        }

        @Override
        public void setEmptyEvent(View emptyView) {
            setEmptyEventClick(emptyView);
        }
    }

    /**
     * 默认监听
     */
    protected OnLoadingListener getOnLoadingListener() {
        return new AppLoadingListener();
    }

    /**
     * 网络失败点击重新调用
     */
    protected void onRetryClick() {
        initNet();
    }

    /**
     * 空页面处理事件
     *
     * @param emptyView
     */
    protected void setEmptyEventClick(View emptyView) {

    }


    /**
     * 空布局
     */
    protected int getEmptyLayoutId() {
        return -1;
    }

    /**
     * 设置错误View的点击
     *
     * @param retryView
     */
    private void setViewClick(View retryView) {
        if (retryView != null) {
            retryView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRetryClick();
                }
            });
        }
    }


    @Override
    public void bindView(View contentView) {
        EventBus.getDefault().register(this);
        // TODO 可以自已添加 ButterKnife
        // mBinder = ButterKnife.bind(this, contentView);
        // 初始化加载页面
        mLoadingLayout = LoadingManager.create(getLoadingParentView(), getOnLoadingListener());
        mLoadingLayout.showContent();
    }

    @Override
    public void unbindView(View contentView) {
        EventBus.getDefault().unregister(this);
        // TODO 可以自已添加 ButterKnife
//        if (mBinder != null) {
//            mBinder.unbind();
//            mBinder = null;
//        }
    }

    /**
     * 防止EventBus报错
     */
    @Subscribe
    public void onEvent(AppFragment fragment) {

    }


}
